﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Srs.Sample.TestService
{
    class Program
    {
        const int testId = 3;
        static void Main(string[] args)
        {

            MainTest();

            //LinqTest();
            Console.WriteLine("Press <Enter> to continue...");
            Console.ReadLine();
        }

        //This is testing Linq to Entities
        static void LinqTest()
        {
            Type customerType = typeof(CustomerService.Customer);
            Type[] typesEx = new Type[] { typeof(string), typeof(int?), typeof(int), typeof(System.Data.Linq.Binary) };

            var props = from prop in customerType.GetProperties()
                        select prop;

            List<PropertyInfo> props2 = (from p in props where typesEx.Contains(p.PropertyType) select p).ToList();

            foreach (PropertyInfo p2 in props2)
            {
                Console.WriteLine(p2.Name);
            }
                           
        }

        private static void MainTest()
        {
            Console.WriteLine("Calling TestTranaction()...");
            using (CustomerService.CustomerServiceClient customerService = new Srs.Sample.TestService.CustomerService.CustomerServiceClient())
            {
                customerService.TestTransaction();
            }

            Console.WriteLine("Calling GetCustomers()...");
            using (CustomerService.CustomerServiceClient customerService = new Srs.Sample.TestService.CustomerService.CustomerServiceClient())
            {
                List<CustomerService.Customer> customers = new List<Srs.Sample.TestService.CustomerService.Customer>(customerService.GetCustomers());
                Console.WriteLine(customers.Count);
                foreach (CustomerService.Customer c in customers)
                    Console.WriteLine(c.ID + ", " + c.Name);
            }

            Console.WriteLine("Calling GetCustomer(), SaveCustomer(), GetCustomer() and DeleteCustomer()...");
            using (CustomerService.CustomerServiceClient customerService = new Srs.Sample.TestService.CustomerService.CustomerServiceClient())
            {
                CustomerService.Customer cust = customerService.GetCustomer(testId);
                cust.Name = "Joe McPeak222";
                customerService.SaveCustomer(cust);
                cust = customerService.GetCustomer(testId);
                //customerService.DeleteCustomer(cust);
            }

            Console.WriteLine("Calling SaveCustomer()...");
            using (CustomerService.CustomerServiceClient customerService = new Srs.Sample.TestService.CustomerService.CustomerServiceClient())
            {
                CustomerService.Customer customer = new Srs.Sample.TestService.CustomerService.Customer();
                customer.Name = "Joe McPeak";
                customerService.SaveCustomer(customer);
            }

            Console.WriteLine("Testing exception propagation and proxy cleanup...");
            try
            {
                using (CustomerService.CustomerServiceClient customerService = new Srs.Sample.TestService.CustomerService.CustomerServiceClient())
                {
                    List<CustomerService.Dealer> dealers = new List<Srs.Sample.TestService.CustomerService.Dealer>(customerService.GetDealersForCustomer(1));
                    Console.WriteLine(dealers.Count);
                    foreach (CustomerService.Dealer d in dealers)
                        Console.WriteLine(d.ID + ", " + d.Name + "," + (d.PhoneNumber == null ? "<null>" : d.PhoneNumber));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}
